package com.shengde.jinwawaforleader.views;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.shengde.jinwawaforleader.R;

/**
 * Created by JYQ on 2017/1/9.
 */

public class SchoolMienPopupWindow extends PopupWindow {
    View mPopView; //popuwindow的 布局页面
    public RelativeLayout relative_share;  //分享
    public RelativeLayout relative_editor_custom;//编辑自定义
    public RelativeLayout relative_editor_link;//编辑第三方链接
    public ImageView iv_editor_link;
    public ImageView iv_editor_custom;
    public TextView tv_editor_link;
    public TextView tv_editor_custom;

    public SchoolMienPopupWindow(Activity context, View.OnClickListener itemsOnClick) {
        super(context);
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mPopView = inflater.inflate(R.layout.pop_layout_dowm, null);
        int widthSize= context.getWindowManager().getDefaultDisplay().getWidth();
        relative_share = (RelativeLayout) mPopView.findViewById(R.id.relative_share);
        relative_editor_custom = (RelativeLayout) mPopView.findViewById(R.id.relative_editor_custom);
        relative_editor_link = (RelativeLayout) mPopView.findViewById(R.id.relative_editor_link);
        ImageView iv_share = (ImageView) mPopView.findViewById(R.id.iv_share);
        iv_editor_link = (ImageView) mPopView.findViewById(R.id.iv_editor_link);
        iv_editor_custom = (ImageView) mPopView.findViewById(R.id.iv_editor_custom);
        TextView tv_share = (TextView) mPopView.findViewById(R.id.tv_share);
        tv_editor_link = (TextView) mPopView.findViewById(R.id.tv_editor_link);
        tv_editor_custom = (TextView) mPopView.findViewById(R.id.tv_editor_custom);


        //设置按钮监听
        relative_share.setOnClickListener(itemsOnClick);
        relative_editor_custom.setOnClickListener(itemsOnClick);
        relative_editor_link.setOnClickListener(itemsOnClick);

        //设置SelectPicPopupWindow的View
        this.setContentView(mPopView);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(widthSize/2);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.AnimationPreview);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(Color.TRANSPARENT);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        //mPopView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        mPopView.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {

                int height = mPopView.findViewById(R.id.linearlayout_pop).getTop();
                int y=(int) event.getY();
                if(event.getAction()==MotionEvent.ACTION_UP){
                    if(y<height){
                        dismiss();
                    }
                }
                return true;
            }
        });



    }
    /**
     * 显示popupWindow
     *
     * @param parent
     */
    public void showPopupWindow(View parent) {
        if (!this.isShowing()) {
            // 以下拉方式显示popupwindow
            this.showAsDropDown(parent, parent.getLayoutParams().width / 2, 5);
        } else {
            this.dismiss();
        }
    }
}
